package com.wondertek.poms.service.listener;

import cn.hutool.core.date.DateUtil;
import com.alibaba.excel.context.AnalysisContext;
import com.alibaba.excel.event.AnalysisEventListener;
import com.wondertek.poms.common.constant.CurrentUser;
import com.wondertek.poms.common.exception.BusinessException;
import com.wondertek.poms.common.exception.MessageCodes;
import com.wondertek.poms.core.service.IContentService;
import com.wondertek.poms.core.service.IPlanMovePrdInfoService;
import com.wondertek.poms.core.service.IPrdContService;
import com.wondertek.poms.core.service.IPrdInfoService;
import com.wondertek.poms.dao.po.Content;
import com.wondertek.poms.dao.po.PlanMovePrdInfo;
import com.wondertek.poms.dao.po.PrdCont;
import com.wondertek.poms.dao.po.PrdInfo;
import com.wondertek.poms.service.vo.PlanMovePrdInfoImportVo;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;

import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;

/**
 * @author evan0213
 * @description
 */
@Slf4j
public class UploadPlanMovePrdInfoListener extends AnalysisEventListener<PlanMovePrdInfoImportVo> {

	private IPlanMovePrdInfoService planMovePrdInfoService;
	private IContentService contentService;
	private IPrdContService prdContService;
	private IPrdInfoService prdInfoService;

	public UploadPlanMovePrdInfoListener(IPlanMovePrdInfoService planMovePrdInfoService, IContentService contentService,
	                                     IPrdContService prdContService, IPrdInfoService prdInfoService) {
		this.planMovePrdInfoService = planMovePrdInfoService;
		this.contentService = contentService;
		this.prdContService = prdContService;
		this.prdInfoService = prdInfoService;
	}

	@Override
	public void invoke(PlanMovePrdInfoImportVo vo, AnalysisContext context) {
		log.info("解析到的计划移包数据{}", vo);
		//excel数据校验
		if (checkImportData(vo)) {
			if (vo.getPrdContId() != null && StringUtils.isBlank(vo.getContentId())) {
				importByPrdContId(vo);
			} else if (vo.getPrdContId() == null && StringUtils.isNotBlank(vo.getContentId())) {
				importByContentId(vo);
			} else {
				throw new BusinessException(MessageCodes.M980303);
			}
		}

	}

	/**
	 * @param vo excel解析出来的数据
	 * @return 校验结果
	 */
	private boolean checkImportData(PlanMovePrdInfoImportVo vo) {
		Long newPrdInfoId = vo.getNewPrdInfoId();
		if (newPrdInfoId == null) {
			throw new BusinessException(MessageCodes.M980306);
		} else {
			PrdInfo prdInfo = prdInfoService.findByPrdInfoId(newPrdInfoId);
			if (prdInfo == null) {
				throw new BusinessException(MessageCodes.M980304);
			}
			if (prdInfo.getIsPublish() != 2) {
				throw new BusinessException(MessageCodes.M980305);
			}
		}
		String planTime = vo.getPlanTime();
		log.info("解析到计划时间为：{}", planTime);
		if (planTime == null) {
			throw new BusinessException(MessageCodes.M980306);
		}
		try {
			SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm aa", Locale.ENGLISH);
			Date time = DateUtil.parse(planTime, df);
		} catch (Exception e) {
			throw new BusinessException(MessageCodes.M980307);
		}
		return true;
	}

	private void importByContentId(PlanMovePrdInfoImportVo vo) {
		log.info("解析到内容id进行计划移包###########");
		String contentId = vo.getContentId();
		Content content = contentService.findByContentId(contentId);
		if (content == null) {
			throw new BusinessException(MessageCodes.M980302);
		}
		PlanMovePrdInfo planMovePrdInfo = new PlanMovePrdInfo();
		planMovePrdInfo.setStatus("0");
		planMovePrdInfo.setCreatePerson(CurrentUser.currUserName());
		planMovePrdInfo.setCreateTime(new Date());
		planMovePrdInfo.setContentId(vo.getContentId());
		planMovePrdInfo.setNewPrdInfoId(vo.getNewPrdInfoId());
		SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm aa", Locale.ENGLISH);
		Date time = DateUtil.parse(vo.getPlanTime(), df);
		planMovePrdInfo.setPlanTime(time);
		planMovePrdInfoService.save(planMovePrdInfo);
	}

	private void importByPrdContId(PlanMovePrdInfoImportVo vo) {
		log.info("解析到节目id进行计划移包##############");
		PrdCont prdCont = prdContService.get(vo.getPrdContId());
		if (prdCont == null) {
			throw new BusinessException(MessageCodes.M980300);
		}
		PlanMovePrdInfo planMovePrdInfo = new PlanMovePrdInfo();
		planMovePrdInfo.setOldPrdInfoId(prdCont.getPrdInfoId());
		planMovePrdInfo.setNewPrdInfoId(vo.getNewPrdInfoId());
		planMovePrdInfo.setPrdContId(vo.getPrdContId());
		planMovePrdInfo.setStatus("0");
		planMovePrdInfo.setCreatePerson(CurrentUser.currUserName());
		planMovePrdInfo.setCreateTime(new Date());
		SimpleDateFormat df = new SimpleDateFormat("yyyy/MM/dd hh:mm aa", Locale.ENGLISH);
		Date time = DateUtil.parse(vo.getPlanTime(), df);
		planMovePrdInfo.setPlanTime(time);
		planMovePrdInfoService.save(planMovePrdInfo);

	}


	@Override
	public void doAfterAllAnalysed(AnalysisContext context) {

	}

}
